package com.qishenyuan.competition.vo;

import com.fasterxml.jackson.core.type.TypeReference;
import com.qishenyuan.competition.Bo.EducationExperience;
import com.qishenyuan.competition.Bo.InternshipExperience;
import com.qishenyuan.competition.Bo.ProjectExperience;
import com.qishenyuan.competition.constant.UserConstant;
import com.qishenyuan.competition.model.User;
import com.qishenyuan.competition.utils.AliOssUtil;
import com.qishenyuan.competition.utils.JacksonUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class UserInfoResp {

    private String uid;

    private String nickname;

    private String avatar;

    private String school;

    private String major;

    private List<ProjectExperience> projectExperiences;

    private List<EducationExperience> educationExperiences;

    private List<InternshipExperience> internshipExperiences;

    private List<String> certificates;

    private String resume;

    private String resumeByAi;

    private String openId;

    private String token;

    public static UserInfoResp buildVo(User user, String token){
        UserInfoResp resp = new UserInfoResp();
        resp.setUid(user.getUid());
        resp.setOpenId(user.getOpenId());
        resp.setNickname(user.getNickName());
        if (UserConstant.DEFAULT_AVATAR.equals(user.getAvatar())) {
            resp.setAvatar(UserConstant.DEFAULT_AVATAR);
        }else {
            resp.setAvatar(AliOssUtil.generatePreSignedUrl(user.getAvatar()));
        }
        resp.setSchool(user.getSchool());
        resp.setMajor(user.getMajor());
        if (StringUtils.isNotBlank(user.getProjectExperience())) {
            resp.setProjectExperiences(JacksonUtil.toObject(user.getProjectExperience(),new TypeReference<List<ProjectExperience>>() {}));
        }
        if (StringUtils.isNotBlank(user.getEducationExperience())) {
            resp.setEducationExperiences(JacksonUtil.toObject(user.getEducationExperience(),new TypeReference<List<EducationExperience>>() {}));
        }
        if (StringUtils.isNotBlank(user.getInternshipExperience())) {
            resp.setInternshipExperiences(JacksonUtil.toObject(user.getInternshipExperience(),new TypeReference<List<InternshipExperience>>() {}));
        }
        if (StringUtils.isNotBlank(user.getCertificates())) {
            resp.setCertificates(string2List(user.getCertificates()));
        }
        resp.setResume(StringUtils.isNotBlank(user.getResume()) ? AliOssUtil.generatePreSignedUrl(user.getResume()) : "");
        resp.setResumeByAi(StringUtils.isNotBlank(user.getResumeByAi()) ? AliOssUtil.generatePreSignedUrl(user.getResumeByAi()) : null);
        resp.setToken(token);
        return resp;
    }

    // 把String转换成list，用“;”分隔开
    private static List<String> string2List(String str) {
        List<String> result = Arrays.asList(str.split(";"));
        result = result.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
        return result;
    }
}
